package auth

import (
	"context"
	"github.com/gogf/gf/v2/encoding/gjson"
	"github.com/gogf/gf/v2/util/gutil"
	"zuhao-seucre/internal/consts"
	"zuhao-seucre/internal/dao"
	"zuhao-seucre/internal/model/entity"

	"zuhao-seucre/api/auth/v1"
)

func (c *ControllerV1) Oauth2Config(ctx context.Context, req *v1.Oauth2ConfigReq) (res *v1.Oauth2ConfigRes, err error) {
	var (
		config entity.TSysConfig
	)
	res = new(v1.Oauth2ConfigRes)
	err = dao.TSysConfig.Ctx(ctx).Where(dao.TSysConfig.Columns().Key, consts.ConfigKeyAccountLoginSecurity).Scan(&config)
	if gutil.IsEmpty(config.Value) {
		res.Enable = false
		res.Name = ""
		return
	}
	// 转化成json
	json := gjson.New(config.Value)
	res.Enable = json.Get("enable").Bool()
	res.Name = json.Get("name").String()
	res.ClientId = json.Get("clientId").String()
	res.ClientSecret = json.Get("clientSecret").String()
	res.AuthorizationURL = json.Get("authorizationURL").String()
	res.AccessTokenURL = json.Get("accessTokenURL").String()
	res.RedirectURL = json.Get("redirectURL").String()
	res.Scopes = json.Get("scopes").String()
	res.ResourceURL = json.Get("resourceURL").String()
	res.UserIdentifier = json.Get("userIdentifier").String()
	res.AutoRegister = json.Get("autoRegister").Bool()
	return
}
